
package com.joysuch.wwyt.datadump.biz.convertor;

import cn.zlg.common.datadump.annotation.DataDumpField;
import cn.zlg.common.datadump.convertor.FieldValueConvert;
import cn.zlg.common.datadump.service.JPABatchQueryPropertyValueService;
import com.joysuch.wwyt.risk.entity.RiskDistrict;
import com.joysuch.wwyt.risk.entity.dsl.QRiskDistrict;
import com.joysuch.wwyt.risk.repository.RiskDistrictDao;
import com.mysema.commons.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * bp_system_video表中，evaluation字段是字符串型，对应ID
 */
@Component
public class ExportSystemVideoEvaluationToRiskDistrictNameConvertor implements FieldValueConvert<String, String> {

    @Autowired
    private RiskDistrictDao dao;
    @Autowired
    private JPABatchQueryPropertyValueService<RiskDistrict, Long , String> queryService;

    @Override
    public String convert(Object bean, String value, DataDumpField dataField) {
        if(value == null) {
            return null;
        }
        Long id = Long.parseLong(value);
        RiskDistrict first = dao.findById(id).orElse(null);
        return first == null ? null : first.getName();
    }

    @Override
    public boolean supportBatch() {
        return true;
    }

    @Override
    public List<String> convert(List<Object> bean, List<String> value, DataDumpField dataDumpField) {
        List<Long> idList = value.stream().map(s -> s == null ? null : Long.parseLong(s)).collect(Collectors.toList());
        QRiskDistrict q = QRiskDistrict.riskDistrict;
        return queryService.query(q, q.id, q.name, idList);
    }
}
